package logic

import (
	"context"

	"github.com/eeppee_admin/clouddisk/service/internal/svc"
	"github.com/eeppee_admin/clouddisk/service/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/tencentyun/cos-go-sdk-v5"
	"github.com/eeppee_admin/clouddisk/service/model"
	"github.com/eeppee_admin/clouddisk/service/helper"

)

type FileChunkUploadDoneLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileChunkUploadDoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileChunkUploadDoneLogic {
	return &FileChunkUploadDoneLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// todo: not test
func (l *FileChunkUploadDoneLogic) FileChunkUploadDone(req *types.FileChunkUploadDoneRequest) (resp *types.FileChunkUploadDoneResponse, err error) {
	co := make([]cos.Object, 0)
	for _, v := range req.CosObjects {
		co = append(co, cos.Object{
			ETag: v.Etag,
			PartNumber: v.PartNumber,
		})
	}

	err = helper.CosPartUploadComplete(req.Key, req.UploadId, co)

	// 数据写入数据库
	sp := &model.StorePool {
		Identity: helper.UUID(),
		Hash: req.Md5,
		Name: req.Name,
		Ext: req.Ext,
		Size: req.Size,
		Path: l.svcCtx.Config.Tencent.BucketUrl + "/" + req.Key,
	}

	l.svcCtx.Engine.Insert(sp)

	return
}
